package utils

import (
	"bufio"
	"bytes"
	"encoding/json"
	"net/http"
	"os/exec"

	"lottery/conf"

	"github.com/astaxie/beego/logs"
)

type Cookie struct {
	Domain   string `json:"domain"`
	Httponly bool   `json:"httponly"`
	Name     string `json:"name"`
	Path     string `json:"path"`
	Secure   bool   `json:"secure"`
	Value    string `json:"value"`
}

func MaxBetLoginEnv(reqUrl, passwd string) (htmlData []byte, cookies []*http.Cookie, passwdEncrypt string, err error) {
	cmd := exec.Command(conf.LoadResource().PhantomJsBin, conf.LoadResource().MaxbetJs, reqUrl, passwd)
	var out bytes.Buffer
	cmd.Stdout = &out
	if err = cmd.Run(); err != nil {
		return
	}

	var cookiesData, passwdEncryptData []byte
	var counter int
	buf := bytes.NewBuffer(make([]byte, 0, 1024))
	scanner := bufio.NewScanner(bytes.NewReader(out.Bytes()))
	for scanner.Scan() {
		line := scanner.Text()
		if line == "<==========>" {
			switch counter {
			case 0:
				htmlData = buf.Bytes()
			case 1:
				cookiesData = buf.Bytes()
			case 2:
				passwdEncryptData = buf.Bytes()
				break
			}

			counter += 1
			buf = bytes.NewBuffer(make([]byte, 0, 1024))
			continue
		}
		buf.WriteString(line)
	}

	myCookies := make([]*Cookie, 0, 10)
	if err = json.Unmarshal(cookiesData, &myCookies); err != nil {
		return
	}

	cookies = myCookies2HttpCookies(myCookies)
	passwdEncrypt = string(passwdEncryptData)
	return
}

func GetCookieISNXFP(reqUrl, cookieData string) (cookies []*http.Cookie, xfp string, err error) {
	cmd := exec.Command(conf.LoadResource().PhantomJsBin, conf.LoadResource().ISNRenderJs, cookieData, reqUrl)
	var out bytes.Buffer
	cmd.Stdout = &out
	if err = cmd.Run(); err != nil {
		return
	}

	var cookiesData []byte
	var xfpData []byte
	scanner := bufio.NewScanner(bytes.NewReader(out.Bytes()))
	buf := bytes.NewBuffer(make([]byte, 0, 1024))
	var counter int
	for scanner.Scan() {
		line := scanner.Text()
		if line == "<==========>" {
			switch counter {
			case 0:
				cookiesData = buf.Bytes()
			case 1:
				xfpData = buf.Bytes()
				break
			}
			counter += 1
			buf = bytes.NewBuffer(make([]byte, 0, 1024))
			continue
		}
		buf.WriteString(line)
	}

	logs.Notice("==========%s", string(cookiesData))

	myCookies := make([]*Cookie, 0, 10)
	if err = json.Unmarshal(cookiesData, &myCookies); err != nil {
		return
	}

	cookies = myCookies2HttpCookies(myCookies)
	xfp = string(xfpData)
	return
}

func GetCookieWithHtml(reqUrl string) (html []byte, cookies []*http.Cookie, err error) {
	cmd := exec.Command(conf.LoadResource().PhantomJsBin, conf.LoadResource().HtmlCookieJs, reqUrl)
	var out bytes.Buffer
	cmd.Stdout = &out
	if err = cmd.Run(); err != nil {
		return
	}

	var cookiesData []byte
	scanner := bufio.NewScanner(bytes.NewReader(out.Bytes()))
	buf := bytes.NewBuffer(make([]byte, 0, 1024))
	var counter int
	for scanner.Scan() {
		line := scanner.Text()
		if line == "<==========>" {
			switch counter {
			case 0:
				html = buf.Bytes()
			case 1:
				cookiesData = buf.Bytes()
				break
			}
			counter += 1
			buf = bytes.NewBuffer(make([]byte, 0, 1024))
			continue
		}
		buf.WriteString(line)
	}

	myCookies := make([]*Cookie, 0, 10)
	if err = json.Unmarshal(cookiesData, &myCookies); err != nil {
		return
	}

	cookies = myCookies2HttpCookies(myCookies)
	return
}

func myCookies2HttpCookies(myCookies []*Cookie) (cookies []*http.Cookie) {
	cookies = make([]*http.Cookie, 0, len(myCookies))
	for _, myCookie := range myCookies {
		cookies = append(cookies, &http.Cookie{
			Name:     myCookie.Name,
			Value:    myCookie.Value,
			Path:     myCookie.Path,
			Domain:   myCookie.Domain,
			Secure:   myCookie.Secure,
			HttpOnly: myCookie.Httponly,
		})
	}
	return
}

func GetCookie(reqUrl string) (cookies []*http.Cookie, err error) {
	return
}
